<template>
  <el-dialog class="dia-style" :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="参数名称" prop="configName">
        <el-input v-model="form.configName" placeholder="请输入参数名称" />
      </el-form-item>
      <el-form-item label="参数键名" prop="configKey">
        <el-input v-model="form.configKey" placeholder="请输入参数键名" />
      </el-form-item>
      <el-form-item label="参数键值" prop="configValue">
        <el-input v-model="form.configValue" placeholder="请输入参数键值" />
      </el-form-item>
      <el-form-item label="系统内置" prop="configType">
        <el-radio-group v-model="form.configType">
          <el-radio v-for="dict in configList" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addConfig, updateConfig } from '@/api/system/config'

export default {
  props: {
    title: {
      type: String,
      default() {
        return '标题'
      },
    },
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
    configList: {
      type: Array,
      default() {
        return []
      },
    },
  },
  components: {},
  data() {
    return {
      open: false,
      form: {},
      // 表单校验
      rules: {
        configName: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
        configKey: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
        configValue: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }],
      },
    }
  },
  watch: {
    formData: {
      handler(newVal) {
        this.form = newVal
      },
      deep: true,
    },
  },
  methods: {
    show() {
      this.open = true
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConfig(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$emit('submitForm')
            })
          } else {
            addConfig(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.$emit('submitForm')
            })
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: 'Y',
        remark: undefined,
      }
      this.resetForm('form')
    },
  },
}
</script>

<style scoped></style>
